import { createRouter, createWebHistory } from 'vue-router'
import { useAdminStore } from '@/stores/admin' // 假设使用Pinia管理用户状态

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'main',
      component: () => import('../views/MainView.vue'),
      redirect: '/dashboard',
      children: [
        // 移动端跳转页
        {
          path: '/admin-redirect',
          name: 'AdminRedirect',
          component: () => import('@/views/AdminRedirect.vue'),
          meta: { requiresAuth: false }
        },
        // 工作台 
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('../views/DashBoard.vue'),
        },
        // 用户管理 
        {
          path: 'userList',
          name: 'UserList',
          component: () => import('../views/UserList.vue'),
        },
        // 文档管理 
        {
          path: 'documentList',
          name: 'Documents',
          component: () => import('../views/DocumentList.vue'),
        },
        // 知识库管理 
        {
          path: 'knowledgeBase',
          name: 'KnowledgeBase',
          component: () => import('../views/KnowledgeBase.vue'),
        },
        // 对话记录管理
        {
          path: 'sessionLog',
          name: 'SessionLog',
          component: () => import('../views/SessionLog.vue'),
        },
        // 对话统计分析
        {
          path: 'sessionStats',
          name: 'SessionStats',
          component: () => import('../views/StatsView.vue'),
        },
        // 个人中心
        {
          path: 'userView',
          name: 'UserView',
          component: () => import('../views/UserView.vue'),
        },
      ]
    }
  ],
})

router.beforeEach((to, from, next) => {
  const adminStore = useAdminStore()

  if (to.meta.requiresAuth) {
    // 检查Pinia或localStorage中是否有token
    if (adminStore.token || localStorage.getItem('adminToken')) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
